package com.copractice.data.dao.hibernate.tag;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.copractice.data.dao.hibernate.HibernateDAO;
import com.copractice.data.dao.tag.TagDAO;
import com.copractice.data.entity.tag.Tag;

/**
 * Implementation of <code>HibernateDAO</code> for <code>Tag</code> class using <code>Hibernate</code> framework.
 * 
 * @author Dmitry Churbanov (dmitry.churbanov@gmail.com)
 * @version 1.0 03/14/2009
 */
public class HibernateTagDAO extends HibernateDAO<Tag, Long> implements TagDAO {

    private static final Logger LOG = LoggerFactory.getLogger(HibernateTagDAO.class);    
    
    /**
     * @see com.copractice.data.dao.hibernate.HibernateDAO#getPersistenceClass
     */    
    @Override
    protected Class<Tag> getPersistenceClass() {
        return Tag.class;
    }
    
    /**
     * @see edu.vt.intelligenttutor.data.dao.tag.TagDAO#findByName(String)
     */
    @SuppressWarnings("unchecked")
    public Tag findByName(String tagName) {
        List<Tag> tags = getSession().getNamedQuery("TAGS.findByTagName").setString(0, tagName).list();
        if (tags.size() == 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Tag '{}' was NOT found", tagName);
            }
            return null;
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Tag '{}' was found", tagName);
            }
            return tags.get(0);
        }
    }    
    
}
